var lodash=require('lodash')/* lodash是一个函数的类库 */

function reducer(state=[],action){
    switch(action.type){
        case 'ADD_TO_CART':
            var pos=lodash.findIndex(state,{id:action.payload.id});
            if(pos !== -1){
                state[pos].quantity = state[pos].quantity+1;
                return [...state]
            }else{
                action.payload.quantity = 1;
                return [...state,action.payload]
            }
           return state;
        case 'DELETEPRO':
            state.splice(action.payload,1)
           return [...state];
        case 'CHANGEQ':
           state[action.payload[0]].quantity=action.payload[1];
          return [...state]
        default:
            return state;
    }
}

export default reducer;